<?php
require_once("includes/pagetop.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>AllyFolio | Blogs </title>
<link href="css/style.css" rel="stylesheet" type="text/css" media="all"  />
<script src="js/general.js"></script>
<script src="js/functions.js"></script>

<link rel="stylesheet" href="alertify/themes/alertify.core.css" />
<link rel="stylesheet" href="alertify/themes/alertify.default.css" id="toggleCSS" />
<script src="alertify/lib/alertify.min.js"></script>	
<script src="js/jquery1.10.2.min.js"></script>

<script src="js/jquery_raty.js"></script>

</head>

<body>

	<div id="main_wrapper">
<?php
    $pageheading="Blogs";
    require_once("includes/header.php");
    if(isset($_REQUEST['t'])&& trim($_REQUEST['t'])!='')
    {
    	$dnauserqry="select * from af_users where Allyfolio_User_ID=:afuserid  ";
		$condition[":afuserid"]=$_REQUEST['t'];
		$dnauserinfo=$pdodbobj->fetch_array($pdodbobj->query($dnauserqry,$condition));
		$userid=$dnauserinfo[0]["User_ID"];
    }
    else
	{
		$userid=$_SESSION['Allyfolio_User_ID'];
	}
    
    if(isset($_REQUEST["msg_success"]) && $_REQUEST["msg_success"]!="")
    {
    	$msg_success=$_REQUEST["msg_success"];
    }
    
	function updateViewCounter($blog_id)
	{	
		global $pdodbobj;
		$timegap=60*60;
		//$timegap=1;
		if (is_bot())
			$isbot = 1;
		else
			$isbot = 0;
		
		if(isset($_SERVER['HTTP_REFERER']))
		{
			$referer=$_SERVER['HTTP_REFERER'];
		}
		else
		{
			$referer=" ";
		}
		
		$strSQL = "SELECT * FROM af_visitor_log where ip_address= '".$_SERVER['REMOTE_ADDR']."' and user_agent='".strtolower($_SERVER['HTTP_USER_AGENT'])."' and visit_date>".(time()-$timegap)." and blog_id='".$blog_id."' and is_bot=".$isbot;
		$resSQL = $pdodbobj->fetch_array($pdodbobj->query($strSQL,array()));
		if(count($resSQL) < 1)
		{
			$strSQL1 = "insert into af_visitor_log (ip_address,referer,user_agent,blog_id,visit_date,is_bot) values ('".$_SERVER['REMOTE_ADDR']."','".$referer."','".strtolower($_SERVER['HTTP_USER_AGENT'])."','".$blog_id."','".time()."','".$isbot."')";
            $pdodbobj->query($strSQL1,array());
			
			$qryUpdateCount="update af_blogs set total_view=(total_view+1) where blog_id='".$blog_id."'";
			$pdodbobj->query($qryUpdateCount,array());
		} 	
	}
	
	function is_bot()
	{
		$botlist = array("Teoma", "alexa", "froogle", "Gigabot", "inktomi","looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory","Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot","crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp","msnbot", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz","Baiduspider", "Feedfetcher-Google", "TechnoratiSnoop", "Rankivabot","Mediapartners-Google", "Sogou web spider", "WebAlta Crawler","TweetmemeBot","Butterfly","Twitturls","Me.dium","Twiceler");
		foreach($botlist as $bot)
		{
			if(strpos($_SERVER['HTTP_USER_AGENT'], $bot) !== false)
				return true;
		}
		return false;
	}
	
	$condition=array(":user_id"=>$userid);
	$qryBlogPageInfo="select * from af_blog_page where user_id =:user_id and status=0";
    $arrBlogPageInfo=$pdodbobj->fetch_array($pdodbobj->query($qryBlogPageInfo,$condition));
    
    if(count($arrBlogPageInfo)>0)
    {
		if(trim($_REQUEST["blog"])!="" && $_REQUEST["blog"]>0)
		{
			$condition=array(":blog_id"=>$_REQUEST["blog"],":user_id"=>$userid);
			//$qryBlog="select * from af_blogs where user_id='".$_SESSION['Allyfolio_User_ID']."' and status=0 and blog_id=:blog_id order by post_date desc limit 1";
			$qryBlog="select * from af_blogs, af_users where status=0 and blog_id=:blog_id and af_blogs.user_id =:user_id and af_blogs.user_id=af_users.User_ID and af_users.Activation_Status='Active' and af_blogs.status!=2 order by post_date desc limit 1";
		}
		else
		{
	    	//$qryBlog="select * from af_blogs where user_id='".$_SESSION['Allyfolio_User_ID']."' and status=0 order by post_date desc limit 1";
	    	$qryBlog="select * from af_blogs, af_users where status=0 and af_blogs.user_id =:user_id and af_blogs.user_id=af_users.User_ID and af_users.Activation_Status='Active' and af_blogs.status!=2 order by post_date desc limit 1";
	    }
	    $arrBlog=$pdodbobj->fetch_array($pdodbobj->query($qryBlog,$condition));
	    //$arrBlog=array();
	    if(count($arrBlog)>0)
	    {
	    
		    //updateViewCounter($arrBlog[0]["blog_id"]);
		    //$arrBlog=$pdodbobj->fetch_array($pdodbobj->query($qryBlog,$condition));
		    
		    $qryBlog="select blog_id from af_blogs, af_users where status=0 and af_blogs.user_id =:user_id and af_blogs.user_id=af_users.User_ID and af_users.Activation_Status='Active' and af_blogs.status!=2 order by post_date desc ";
		    $arrBlogDropDown=$pdodbobj->fetch_array($pdodbobj->query($qryBlog,$condition));
		    
			$qryLinks="select * from  af_blog_links where blog_id='".$arrBlog[0]["blog_id"]."'";
			$arrBlogLinks=$pdodbobj->fetch_array($pdodbobj->query($qryLinks,array()));
			
		    //$qryNextBlog="select blog_id from af_blogs where user_id='".$_SESSION['Allyfolio_User_ID']."' and status=0 and post_date<'".$arrBlog[0]["post_date"]."' order by post_date desc limit 1";
		    $qryNextBlog="select blog_id from af_blogs,af_users where status=0 and af_blogs.user_id =:user_id and af_blogs.user_id=af_users.User_ID and af_users.Activation_Status='Active' and af_blogs.status!=2 and post_date<'".$arrBlog[0]["post_date"]."' order by post_date desc limit 1";
		    $arrNextBlog=$pdodbobj->fetch_array($pdodbobj->query($qryNextBlog,$condition));
	    }
	}
    else
    {
		$arrBlog=array();
		$arrBlogDropDown=array();
		$arrBlogLinks=array();
		$arrNextBlog=array();
	}
?>
    
    
    <!--=================Body Contain Start=======================-->
    <style>
		.big_blog_top ul li 
		{
			width:100%;
		}
		.error_span
		{
			color: #FF0000;
		}
		/*
		.big_blog {
			height: 100% !important;	
		}
		.big_blog_top {
			height: 100% !important;	
		}*/
	</style>
    	<div class="wrapper">
			<div class="new_blog">
				<div class="blog_blank_div">
					<img id="blog_image" src="<?php if($arrBlogPageInfo[0]["blog_page_image"]!=""){ echo "uploads/image_crop.php?image=/uploads/blog_images/".$arrBlogPageInfo[0]["blog_page_image"]."&width=982&height=571&cropratio=982:571"; } else { echo "uploads/image_crop.php?image=/images/upload.png&width=982&height=571&cropratio=982:571"; } ?>" /><div id="image_loader_div" style="display:none;"><img src="images/LoadingBar.gif" width="64px" height="64px"/></div>
				</div>
				<div class="post_upload">
					<div class="post_upload_left">
					<?php
						if(isset($_SESSION['Allyfolio_User_ID']) && $_SESSION['Allyfolio_User_ID']!="" && $userid==$_SESSION['Allyfolio_User_ID'])
						{
					?>
						<div class="blog_btn_txt">
							<form enctype="multipart/form-data" id="image_form" name="image_form" method="post" onsubmit="return checkValidation();">
								<input type="file" id="selectedFile" name="selectedFile" style="display: none;" onChange="upload();" accept="image/*"/><a href="javascript:void(0)" onclick="return openFile();">Upload Image</a>
							</form>
						</div>
					<?php		
						}
					?>
						<!--<div class="blog_btn_txt">
							Upload Link
						</div>-->
					<?php
						if(isset($_SESSION['Allyfolio_User_ID']) && $_SESSION['Allyfolio_User_ID']!="" && $arrBlog[0]["user_id"]==$_SESSION['Allyfolio_User_ID'])
						{
					?>
						<div class="blog_btn_txt">
							<a href="add_blog.php?blog_id=<?php echo $arrBlog[0]["blog_id"]; ?>">Edit Blog</a>
						</div>
					<?php		
						}
					?>	
					</div>
					<div class="post_upload_right">
				<?php
					if($arrBlogDropDown[0]["blog_id"]!=$arrBlog[0]["blog_id"])
					{
				?>	
						<div class="blog_btn_txt">
							<a href="blog.php">Recent post</a>
						</div>
				<?php
					}
				?>	
					<?php
						if(is_array($arrBlog) && $arrBlog[0]["blog_id"]>0)
						{
					?>	
						<div class="blog_btn_txt">
						<?php
							//Check Already Picked
							$qryCheckCurrentPicked="select * from  af_blog_pick where blog_id=:blog_id and user_id=:user_id";
							$condition=array();
							$condition[":user_id"]=$_SESSION['Allyfolio_User_ID'];
							$condition[":blog_id"]=$arrBlog[0]["blog_id"];
							$arrCheckCurrentPicked=$pdodbobj->fetch_array($pdodbobj->query($qryCheckCurrentPicked,$condition));
							?>
							<a href="javascript:void(0)" onclick="return togglePickUnpick('<?php echo $arrBlog[0]['blog_id']; ?>')"><span id="pick_span_<?php echo $arrBlog[0]['blog_id']; ?>">
							<?php
							if(count($arrCheckCurrentPicked)>0) //already picked
							{
						?>
							UnPick
						<?php		
							}
							else
							{
						?>
							Pick
						<?php		
							}
						?>
							</a>
						<?php
							//Get Pick Count
							$qryBlogPickCount="select count(*) as countPickCount from af_blog_pick,af_users where blog_id=:blog_id and af_users.Activation_Status='Active' and af_blog_pick.user_id=af_users.User_ID "; 
							$condition=array();
							$condition[":blog_id"]=$arrBlog[0]["blog_id"];
							$arrBlogPickCount=$pdodbobj->fetch_array($pdodbobj->query($qryBlogPickCount,$condition));
						?>	
							<span id="pick_count_span_<?php echo $arrBlog[0]['blog_id']; ?>" style="margin-left:10px">
						<?php
							if(is_array($arrBlogPickCount) && $arrBlogPickCount[0]["countPickCount"]>0)
							{
								echo $arrBlogPickCount[0]["countPickCount"];		
							}
							else
							{
								echo "0";		
							}
						?>	
							</span>
						</div>
					<?php
						}
					?>	
					<?php
						if(isset($_SESSION['Allyfolio_User_ID']) && $_SESSION['Allyfolio_User_ID']!="" && $arrBlog[0]["user_id"]!=$_SESSION['Allyfolio_User_ID'])
						{
					?>	
						<div class="blog_btn_txt">
						<?php
							//Check Already Followed
							$qryCheckCurrentFollowed="select * from  af_blog_page_follow where blog_page_id=:blog_page_id and user_id=:user_id";
							$condition=array();
							$condition[":user_id"]=$_SESSION['Allyfolio_User_ID'];
							$condition[":blog_page_id"]=$arrBlog[0]["blog_page_id"];
							$arrCheckCurrentFollowed=$pdodbobj->fetch_array($pdodbobj->query($qryCheckCurrentFollowed,$condition));
							?>
							<a href="javascript:void(0)" onclick="return toggleFollowUnfollow('<?php echo $arrBlog[0]['blog_page_id']; ?>')"><span id="follow_span_<?php echo $arrBlog[0]['blog_page_id']; ?>">
							<?php
							if(count($arrCheckCurrentFollowed)>0) //already picked
							{
						?>
							UnFollow
						<?php		
							}
							else
							{
						?>
							Follow
						<?php		
							}
						?>
							</a> <img src="images/side_arrow.png" style="float:right; margin-left:5px;" />
						</div>
					<?php
						}
					?>	
						<?php 
							if(count($arrBlogLinks)==1)
							{
						?>
						<div class="blog_btn_txt">
							<a href="<?php echo $arrBlogLinks[0]["blog_link"] ?>" target="_blank">Links</a>
						</div>
						<?php		
							}
							else if(count($arrBlogLinks)>1)
							{
						?>
						<div class="blog_btn_txt">
							<a href="javascript:void(0);" onclick="return openLinksPopup('<?php echo $arrBlog[0]["blog_id"]; ?>');">Links</a>
						</div>
						<?php		
							}
						?>
						<?php
							if(count($arrBlogDropDown)>1)
							{
						?>
						<div class="blog_btn_txt">
							<select onchange="return displayChoosedBlog();" id="blog_counter_select" name="blog_counter_select">
							<?php		
								for($i=0;$i<count($arrBlogDropDown);$i++)
								{
							?>
								<option value="<?php echo $arrBlogDropDown[$i]["blog_id"]; ?>" <?php if($arrBlogDropDown[$i]["blog_id"]==$arrBlog[0]["blog_id"]){ echo "selected"; } ?>><?php echo ($i+1); ?></option>
							<?php		
								}
							?>
							</select>
						</div>
						<?php		
							}
						?>
						<!--<div class="blog_btn_txt">
							
							<span ><?php //echo $arrBlog[0]["total_view"] ?></span><img src="images/down_arrow2.png" style="float:right; margin-left:5px; margin-top:3px;" />
						</div>-->
					</div>
					<div class="clearfix"></div>
				</div>
		<?php
    		if(count($arrBlog)>0)
    		{
    	?>		
				<div class="blog_head">
					<?php echo $arrBlog[0]["blog_title"]; ?>
				</div>
				<div class="big_blog">
					
					<div class="big_blog_top">
						<ul>
							<li class="big_blog_top_span"><?php echo $arrBlog[0]["blog_description"]; ?></li>
							<!--<li><div class="recent_post"><span>abc recent post</span></div></li>
							<li><div class="recent_post" style="margin-left:50px;"><span>https//:www.abc.com</span></div></li>-->
						</ul>
					</div>
					<!--<div class="upload_pic_vid">
						<div class="recent_post2" style="margin-left:50px;"><span>Upload Photos/Video</span></div>
					</div>-->
				</div>
				
				
			</div>
			<?php
				if(count($arrNextBlog)>0)
				{
			?>
			<div class="more">
				<a href="blog.php?blog=<?php echo $arrNextBlog[0]["blog_id"]; ?>">More...</a>
			</div>
			<?php
				}
			?>
  		<?php
  		}
  		else
  		{
		?>
			<div align="center" style="width:100%"><span class="error_span">Sorry, there is no blogs.</span></div>
		<?php	
		}
  		?>
     	</div>
            
    <!--=================Body Contain end=======================-->
    </div>
    <div style="width: 100%;height:100%;position:fixed;left: 0px;top:0px;display:none;" id="links_div">
    </div>
    <?php
    require_once("includes/footer.php");
?>
<script>
function togglePickUnpick(blogid)
{
	var xmlhttp;
	var ajaxURL="ajax_dna_profile.php?purpose=blogpick&blog_id="+blogid;
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{	
			var res=xmlhttp.responseText;	
			resArr=res.split("|~|");
			if(resArr[0]=="SUCCESS")
			{
				document.getElementById("pick_span_"+blogid).innerHTML=resArr[1];
				document.getElementById("pick_count_span_"+blogid).innerHTML=resArr[2];
			}
			else
			{
				alertify.alert(resArr[1]);
			}
		}
	}	
	xmlhttp.open("POST",ajaxURL,true);
	xmlhttp.send();
}

function toggleFollowUnfollow(blog_page_id)
{
	var xmlhttp;
	var ajaxURL="ajax_dna_profile.php?purpose=blog_page_follow&blog_page_id="+blog_page_id;
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{	
			var res=xmlhttp.responseText;	
			resArr=res.split("|~|");
			if(resArr[0]=="SUCCESS")
			{
				document.getElementById("follow_span_"+blog_page_id).innerHTML=resArr[1];
			}
			else
			{
				alertify.alert(resArr[1]);
			}
		}
	}	
	xmlhttp.open("POST",ajaxURL,true);
	xmlhttp.send();
}


function openFile()
{
	$("#selectedFile").trigger("click");
	//document.getElementById('selectedFile').click(); 
}
function upload()
{
	if(document.getElementById("selectedFile").value!="")
	{
		document.getElementById("image_loader_div").style.display="";
		formdata = false;
		if (window.FormData) {
			formdata = new FormData(document.getElementById("image_form"));
		}
	
		var xmlhttp;
				
		if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{	
				var res=xmlhttp.responseText;	
				resArr=res.split("|~|");
				if(resArr[0]=="SUCCESS")
				{
					//document.getElementById("blog_display_img").src="uploads/temp_blog_images/"+resArr[1];
					document.getElementById("blog_image").src="uploads/image_crop.php?image=/uploads/blog_images/"+resArr[1]+"&width=982&height=571&cropratio=982:571";
					document.getElementById("image_loader_div").style.display="none";
				}
				else
				{
					document.getElementById("image_loader_div").style.display="none";
					alertify.alert(resArr[1]);
				}
			}
		}	
		xmlhttp.open("POST","ajax_add_blog.php",true);
		xmlhttp.send(formdata);
	}
}
function displayChoosedBlog()
{
	window.location.href="blog.php?blog="+document.getElementById("blog_counter_select").value;
}
function openLinksPopup(blog_id)
{
	var xmlhttp;
	var ajaxURL="ajax_add_blog.php?purpose=showlinks&blog_id="+blog_id;
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{	
			var res=xmlhttp.responseText;	
			document.getElementById("links_div").innerHTML=res;
			document.getElementById("links_div").style.display="";
		}
	}	
	xmlhttp.open("POST",ajaxURL,true);
	xmlhttp.send();
}

<?php
if(isset($msg_success) && $msg_success!="")
{
    ?>
    alertify.alert("<?php echo $msg_success; ?>");
    <?php
}

if(isset($msg_error) && $msg_error!="")
{
    ?>
    alertify.alert("<?php echo $msg_error; ?>");
    <?php
}
?>
</script>